package vn.ugame.model.bl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import vn.ugame.exception.DataConnectionFailed;
import vn.ugame.exception.InvalidEntityClass;
import vn.ugame.model.dal.ConfigDA;
import vn.ugame.model.entity.Config;

public class Configuration {

    private static Configuration instance;
    private List<Config> configList;
    private boolean loaded;

    public static Configuration getInstance() {
        if (instance == null) {
            instance = new Configuration();
        }
        return instance;
    }

    private Configuration() {
        configList = new ArrayList<Config>();
    }

    public boolean isLoaded() {
        return loaded;
    }

    public void load() throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ConfigDA configDA = new ConfigDA();
        configList = configDA.findConfigs(null, null);
        this.loaded = true;
    }

    public String getProperty(Locale locale, String key)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {

        for (Config config : configList) {
            if (config.getLocale().getLanguage().equals(locale.getLanguage())
                    && config.getKey().equals(key)) {
                return config.getValue();
            }
        }

        return null;
    }
}
